using System.Diagnostics;

namespace ORM
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

        private PersonDb _personDb;
        public PersonDb DBObject
        {
            get { return _personDb; }
            set { if (_personDb == null) { _personDb = value; } }
        }

        public Person() : this("", "", 0, null) { }

        public Person(string firstName, string lastName, int personsAge)
            : this(firstName, lastName, personsAge, null) { }

        private Person(string firstName, string lastName, int age, PersonDb personDb)
        {
            FirstName = firstName;
            LastName = lastName;
            Age = age;
            DBObject = personDb;
        }
        public void Remove() 
        { 
            _personDb = null; 
        }

        public void Save() 
        { 
            if (_personDb != null) 
                _personDb.Save(this); 
        }

        public void Load()
        {
            if (_personDb == null) return;
            if (_personDb.Id < 0)
                Trace.TraceWarning(" --> Persons db id < 0");
            var person = _personDb.Load();
            Age = person.Age;
            FirstName = person.FirstName;
            LastName = person.LastName;
        }
    }
}